package com.demo.testvideoview;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.net.NetworkRequest;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.widget.VideoView;
import android.view.Window;

public class MainActivity extends AppCompatActivity implements MediaControllerView.MediaControllerButton {

    private static final String TAG = MainActivity.class.getSimpleName();
    VideoView vv;
    MediaControllerView mediaControllerView;


    RelativeLayout layoutVideo;

    Button but_back;
    String url = "https://m.muho.tv/128156_1520582999.mp4";



    MediaController mc;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
       // isMobile();

        initTest();
    }
    ConnectivityManager connectivityManager;
    private void initTest() {

        NetworkRequest.Builder builder = new NetworkRequest.Builder();
        NetworkRequest request = builder.build();
        connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        connectivityManager.registerNetworkCallback(request, new ConnectivityManager.NetworkCallback(){

            @Override
            public void onAvailable(Network network) {
                super.onAvailable(network);



            }

            @Override
            public void onCapabilitiesChanged(Network network, NetworkCapabilities networkCapabilities) {
                super.onCapabilitiesChanged(network, networkCapabilities);
            }
        });

    }

    private void init() {

        mediaControllerView = new MediaControllerView(this,false);
        mediaControllerView.setMediaControllerButton(this);
        layoutVideo = findViewById(R.id.layout_video);
        vv = findViewById(R.id.videoview);
        vv.setVideoURI(Uri.parse(url));
        vv.setMediaController(mediaControllerView);
        // vv.setMediaController(new MediaController(this));
         vv.start();

        but_back = findViewById(R.id.but_back);
        but_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            }
        });
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);



        if( Configuration.ORIENTATION_LANDSCAPE == newConfig.orientation){
            //横屏
            Log.e(TAG,"横屏****************************");


            LinearLayout.LayoutParams par = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
            layoutVideo.setLayoutParams(par);

            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT
            );
            layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
            vv.setLayoutParams(layoutParams);



        }else if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){

            Log.e(TAG,"竖屏****************************");
            int height = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,200,getResources().getDisplayMetrics());
            LinearLayout.LayoutParams par = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,height);
            layoutVideo.setLayoutParams(par);

            //重新设置屏幕自动旋转
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
        }

    }

    @Override
    public void next() {

    }

    @Override
    public void prev() {

    }

    @Override
    public void fullscreen() {

        //请求横屏，设置后屏幕根据传感器自动旋转会失效
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);


    }



    public void isMobile(){

        ConnectivityManager mConnectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo wifiInfo = mConnectivityManager
                .getNetworkInfo(ConnectivityManager.TYPE_WIFI);

//        if(wifiInfo!=null && wifiInfo.isConnected()){
//            Toast.makeText(this,"wifi已连接",Toast.LENGTH_LONG).show();
//        }else{
//            Toast.makeText(this,"wifi不可用",Toast.LENGTH_LONG).show();
//        }


        NetworkInfo mobileInfo = mConnectivityManager
                .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        if(mobileInfo!=null && mobileInfo.isConnected()){
            Toast.makeText(this,"移动网络已连接",Toast.LENGTH_LONG).show();
        }else{
            Toast.makeText(this,"移动网络不可用",Toast.LENGTH_LONG).show();
        }

        BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {

             if(ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())){
                 NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
                 if(info!=null){

                     if(ConnectivityManager.TYPE_MOBILE == info.getType() && info.isConnected()){

                         Toast.makeText(MainActivity.this,"移动网络已连接",Toast.LENGTH_LONG).show();
                     }

                     Log.e(TAG, "onReceive: "+info.getTypeName() +" getState="+ info.getState()+" isConnected="+info.isConnected());
                 }


             }

            }
        };

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(broadcastReceiver,intentFilter);

    }

}
